﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using WebMassage.Models;
using WebMassage.Helpers;

namespace WebMassage.Controllers
{
    public class NewsController : Controller
    {
        NewResponsitory newRes = new NewResponsitory();

        private static int defaultPageSize = 10;
        private string contentId = "ctl00$MainContent$Content";

        public ActionResult Index(int? page)
        {
            try
            {
                NewResponsitory newRes = new NewResponsitory();
                var news = newRes.All();

                int pageIndex = page.HasValue ? page.Value - 1 : 0;
                return View(news.ToPagedList(pageIndex, defaultPageSize));
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        public ActionResult Details(string id)
        {
            try
            {
                New n = newRes.GetById(id);
                if (n != null)
                {
                    return View(n);
                }
                return View("Manager");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Manager(int? page)
        {
            try
            {
                NewResponsitory newRes = new NewResponsitory();
                var news = newRes.All();

                int pageIndex = page.HasValue ? page.Value - 1 : 0;
                return View(news.ToPagedList(pageIndex, defaultPageSize));
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [Authorize(Roles = "Administrator")]
        [ValidateInput(false)]
        public ActionResult Create(string id)
        {
            try
            {
                New n = new New();
                n.CreatedDate = DateTime.Now;
                return View(n);
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        [ValidateInput(true)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                New n = new New();
                UpdateModel(n);
                n.NewID = Guid.NewGuid();
                n.Content = Request.Form.Get(contentId);
                n.CreatedDate = DateTime.Now;
                newRes.Add(n);
                newRes.Save();
                return RedirectToAction("Manager");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [Authorize(Roles = "Administrator")]
        [ValidateInput(false)]
        public ActionResult Edit(string id)
        {
            try
            {
                New n = newRes.GetById(id);
                if (n != null)
                {
                    n.CreatedDate = DateTime.Now;
                    return View(n);
                }
                return View("Manager");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        [ValidateInput(false)]
        public ActionResult Edit(string id, FormCollection collection)
        {
            try
            {
                New n = newRes.GetById(id);
                if (n != null)
                {
                    UpdateModel(n);
                    n.Content = Request.Form.Get(contentId);
                    newRes.Save();
                }
                return RedirectToAction("Manager");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(string id, int? page)
        {
            try
            {
                New n = newRes.GetById(id);
                if (n != null)
                {
                    newRes.Delete(n);
                    newRes.Save();
                }
                return RedirectToAction("Manager", new { page = page });
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View("/Shared/Error");
            }
        }


    }
}
